<template>
  <div class="goods">
    <div class="wrap">
      <Crumb> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            @click="sortClick(index, item.min, item.max)"
            :class="{ active: activeIndex == index ? true : false }"
            v-for="(item, index) in optionsSorting"
            :key="index"
            >{{ item.text }}</span
          >
        </li>
        <li>
          <strong>分类：</strong>
          <span
            @click="typeClick(item.type, index)"
            v-for="(item, index) in optionsType"
            :key="index"
            :class="{ active: item.isActive }"
            >{{ item.text }}</span
          >
        </li>
      </ul>
      <List :arr="showGoodsList" />
      <h3 style="text-align: center">
        {{ isShowBottom ? "正在加载....." : "已经到底了" }}
      </h3>
    </div>
  </div>
</template>
<script>
import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { getGoodsListAPI } from "@/request/api";
import { getScrollHeight, getClientHeight, getScrollTop } from "@/utils";
export default {
  data() {
    return {
      //栏目 目前可提供的参数是 1->精品推荐 2-> 热门兑换 0->全部
      did: 0,
      //( 1->实体商品 2->虚拟商品 0->全部 )
      type: 0,
      // 大于多少积分，最少是0
      min: 0,
      //少于多少积分，最多是10000，当传入0的时候，会直接返回所有商品，无视后台逻辑。
      max: 0,
      //商品关键词
      keyword: "",
      //服务器返回的商品信息
      goodsList: [],
      //排序选项数据
      optionsSorting: [
        { min: 0, max: 0, text: "全部" },
        { min: 0, max: 0, text: "我还可以兑换" },
        { min: 0, max: 500, text: "0-500" },
        { min: 500, max: 1000, text: "500 -1000" },
        { min: 1000, max: 1500, text: "1000-1500" },
        { min: 1500, max: 2500, text: "1500-2500" },
        { min: 2000, max: 6500, text: "2000-6500" },
        { min: 6500, max: 10000, text: "6500-10000" },
      ],
      //当前选中的排序的索引
      activeIndex: 0,
      //分类选项数据
      optionsType: [
        {
          text: "全部",
          type: 0,
          isActive: true,
        },
        {
          text: "实物礼品",
          type: 1,
          isActive: false,
        },
        {
          text: "虚拟礼品",
          type: 2,
          isActive: false,
        },
      ],
      //展示的商品列表
      showGoodsList: [],
      //是否加载中
      isLoading: false,
      //当前页
      page: 1,
      //每页显示的商品数量
      size: 8,
      //是否显示底部提示信息
      isShowBottom: true,
    };
  },
  mounted() {
    //在页面挂载完成阶段添加滚动事件
    window.addEventListener("scroll", this.scrollFn);
  },
  created() {
    //页面加载时从访问地址栏截取搜索关键字实现搜索
    this.keyword = this.$route.query.keyword;
    this.getGoodsListFn();
  },
  beforeDestroy() {
    //在页面销毁之前取消事件监听
    window.removeEventListener(
      "scroll",

      this.scrollFn
    );
    console.log("事件取消");
  },
  watch: {
    //解决在商品页面输入关键字点搜索商品页面不从新加载无法搜索问题
    "$route.query.keyword": {
      handler(newVal, oldVal) {
        if (newVal != oldVal) {
          this.keyword = newVal;
          this.getGoodsListFn();
        }
      },
    },
  },
  methods: {
    //滚动事件函数
    scrollFn() {
      /*
      //可视区域高度
      getScrollHeight
     整个页面文档的攻读
      getClientHeight
      滚出屏幕的高度
      getScrollTop
      */

      //如果可视区域高度+滚出高度<=整个文档高度-50 那么不加载剩余，否则加载
      if (!(getScrollHeight() + getScrollTop() >= getClientHeight() - 20))
        return;
      //节流：使用一个状态值判断，如果当前正在加载则不添加事件
      if (this.isLoading) return;
      console.log("进入加载***********");

      //改变状态为加载中
      this.isLoading = true;
      this.page++;
      setTimeout(() => {
        for (
          let i = (this.page - 1) * this.size;
          i < this.page * this.size;
          i++
        ) {
          this.goodsList[i] && this.showGoodsList.push(this.goodsList[i]);
        }
        //改变状态为不在加载中
        this.isLoading = false;
        if (this.goodsList.length <= this.showGoodsList.length) {
          this.isShowBottom = false;
        } else {
          this.isShowBottom = true;
        }
      }, 500);

      console.log("执滚动函数");
    },
    //点击分类的函数
    typeClick(type, index) {
      this.type = type;
      this.optionsType.forEach((e) => (e.isActive = false));
      this.optionsType[index].isActive = true;
      this.getGoodsListFn();
    },
    //点击排序的函数
    sortClick(index, min, max) {
      //存入当前点击的索引
      this.activeIndex = index;
      this.min = min;
      this.max = max;
      this.getGoodsListFn();
    },
    //获取商品列表函数
    async getGoodsListFn() {
      let res = await getGoodsListAPI({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      });
      if (!res) return;

      this.goodsList = res.data;
      this.showGoodsList = this.goodsList.filter((e, i) => {
        return i < 8;
      });
      //解决在切换排序再切回全部，page不初始化问题、

      this.page = 1;

      if (this.goodsList.length <= this.showGoodsList.length) {
        this.isShowBottom = false;
      } else {
        this.isShowBottom = true;
      }
    },
  },
  components: {
    Crumb,
    List,
  },
};
</script>

<style lang="less" scoped>
@import "../assets/css/public.less";

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
}
</style>
